package com.raycloud.robot.utils;

import java.time.chrono.ChronoLocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.Instant;
import java.util.Objects;
import java.time.LocalDateTime;
import com.raycloud.robot.domain.dto.ApplyTimeDto;

public class ActivityTimeCheckUtil{
    public static boolean checkActivityTime(final Integer activityType,final Long startTime,final Long endTime,final Integer day,final ApplyTimeDto applyTimeDto){
        paramsCheck(activityType,startTime,endTime,day,applyTimeDto);
        LocalDateTime now=LocalDateTime.now();
        if(activityType==0){
            if(Objects.nonNull(applyTimeDto)){
                return ApplyTimeUtil.applyTimeCheck(applyTimeDto,System.currentTimeMillis());
            }
            final LocalDateTime startDate=Instant.ofEpochMilli(startTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            final LocalDateTime endDate=Instant.ofEpochMilli(endTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            return now.isAfter(startDate)&&now.isBefore(endDate);
        }
        else{
            if(activityType==2){
                final LocalDateTime startDate=Instant.ofEpochMilli(startTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
                final LocalDateTime endDate=Instant.ofEpochMilli(endTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
                return now.isAfter(startDate)&&now.isBefore(endDate);
            }
            if(activityType!=1){
                return false;
            }
            final LocalDateTime startDate=Instant.ofEpochMilli(startTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            if(now.isAfter(startDate)||now.isEqual(startDate)){
                return false;
            }
            now=now.plusDays(day);
            return now.isAfter(startDate);
        }
    }
    private static void paramsCheck(final Integer activityType,final Long startTime,final Long endTime,final Integer day,final ApplyTimeDto applyTimeDto){
        if(activityType==null){
            throw new IllegalArgumentException("activityType is null");
        }
        if((startTime==null||startTime<0L)&&Objects.isNull(applyTimeDto)){
            throw new IllegalArgumentException("startTime is null or less 0 or \u65f6\u6548\u6570\u636e\u4e3a\u7a7a");
        }
        if((endTime==null||endTime<0L)&&Objects.isNull(applyTimeDto)){
            throw new IllegalArgumentException("endTime is null or less 0 or \u65f6\u6548\u6570\u636e\u4e3a\u7a7a");
        }
        if(activityType==1&&(day==null||day<0)){
            throw new IllegalArgumentException("day is null or less 0");
        }
    }
    public static void main(final String[] args){
        final Integer type=1;
        final Long startTime=1609171200000L;
        final Long endTime=1609257600000L;
        final Integer day=2;
        final boolean b=checkActivityTime(type,startTime,endTime,day,null);
        System.out.println(b);
    }
}
